package com.europe.business.europebusiness.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.europe.business.europebusiness.R;
import com.europe.business.europebusiness.ui.bean.Section;

import java.util.HashMap;
import java.util.List;

public class SectionAdapter extends RecyclerView.Adapter<SectionAdapter.SectionViewHolder> {

    private Context mContext;
    private List<Section> mData;
    private int mFlag;
    private HashMap<Integer, Integer> hashMap;

    OnSectionClickListener onSectionClickListener;

    public SectionAdapter(Context mContext, List<Section> mData, int mFlag) {
        this.mContext = mContext;
        this.mData = mData;
        this.mFlag = mFlag;
        hashMap = new HashMap<Integer, Integer>();
    }

    public void setOnSectionClickListener(OnSectionClickListener onSectionClickListener) {
        this.onSectionClickListener = onSectionClickListener;
    }

    @NonNull
    @Override
    public SectionViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View itewView = LayoutInflater.from(mContext).inflate(R.layout.item_section, viewGroup, false);
        SectionViewHolder sectionViewHolder = new SectionViewHolder(itewView);
        return sectionViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull SectionViewHolder sectionViewHolder, final int i) {
        if (hashMap.size() > 0) {
            int sectionId = hashMap.get(mFlag);
            if (sectionId == mData.get(i).getSectionId()) {
                sectionViewHolder.sectionSelect.setVisibility(View.VISIBLE);
                if (mFlag == 1) {
                    sectionViewHolder.sectionRoot.setBackgroundColor(mContext.getResources().getColor(R.color.white));
                } else {
                    sectionViewHolder.sectionRoot.setBackgroundColor(mContext.getResources().getColor(R.color.bg_color));
                }
            } else {
                sectionViewHolder.sectionSelect.setVisibility(View.GONE);
                if (mFlag == 1) {
                    sectionViewHolder.sectionRoot.setBackgroundColor(mContext.getResources().getColor(R.color.bg_color));
                } else {
                    sectionViewHolder.sectionRoot.setBackgroundColor(mContext.getResources().getColor(R.color.white));
                }
            }
        } else {
            sectionViewHolder.sectionSelect.setVisibility(View.GONE);
        }
        sectionViewHolder.sectionName.setText(mData.get(i).getSectionName());
        sectionViewHolder.sectionRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hashMap.put(mFlag, mData.get(i).getSectionId());
                onSectionClickListener.onSectionClick(mData.get(i).getSectionId(), mFlag);
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class SectionViewHolder extends RecyclerView.ViewHolder {
        public LinearLayout sectionRoot;
        public ImageView sectionSelect;
        public TextView sectionName;

        public SectionViewHolder(@NonNull View itemView) {
            super(itemView);
            sectionRoot = itemView.findViewById(R.id.section_root);
            sectionSelect = itemView.findViewById(R.id.section_select);
            sectionName = itemView.findViewById(R.id.section_name);

        }
    }

    public interface OnSectionClickListener {
        void onSectionClick(int sectionId, int flag);
    }
}
